{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tkinter \n",
    "import tkinter.messagebox \n",
    "import tkinter.ttk \n",
    " \n",
    "#创建tkinter应用程序 \n",
    "root = tkinter.Tk() \n",
    "#设置窗口标题 \n",
    "root.title('Selection widgets') \n",
    "#定义窗口大小 \n",
    "root['height'] = 400 \n",
    "root['width'] = 320 \n",
    " \n",
    "#与姓名关联的变量 \n",
    "varName = tkinter.StringVar() \n",
    "varName.set('') \n",
    "#创建标签，然后放到窗口上 \n",
    "labelName = tkinter.Label(root, \n",
    "                            text='Name:', \n",
    "                            justify=tkinter.RIGHT, \n",
    "                            width=50) \n",
    "labelName.place(x=10, y=5, width=50, height=20) \n",
    "#创建文本框，同时设置关联的变量 \n",
    "entryName = tkinter.Entry(root, \n",
    "                             width=120, \n",
    "                             textvariable=varName) \n",
    "entryName.place(x=70, y=5, width=120, height=20) \n",
    " \n",
    "labelGrade = tkinter.Label(root, \n",
    "                             text='Grade:', \n",
    "                             justify=tkinter.RIGHT, width=50) \n",
    "labelGrade.place(x=10, y=40, width=50, height=20) \n",
    "#模拟学生所在年级，字典键为年级，字典值为班级 \n",
    "studentClasses = {'1':['1', '2', '3', '4'], \n",
    "                    '2':['1', '2'], \n",
    "                    '3':['1', '2', '3']} \n",
    " #学生年级组合框 \n",
    "comboGrade = tkinter.ttk.Combobox(root,width=50, \n",
    "                              values=tuple(studentClasses.keys())) \n",
    "comboGrade.place(x=70, y=40, width=50, height=20) \n",
    "#事件处理函数 \n",
    "def comboChange(event): \n",
    "    grade = comboGrade.get() \n",
    "    if grade: \n",
    "        #动态改变组合框可选项 \n",
    "                comboClass[\"values\"] = studentClasses.get(grade) \n",
    "    else: \n",
    "        comboClass.set([]) \n",
    "#绑定组合框事件处理函数 \n",
    "comboGrade.bind('<<ComboboxSelected>>', comboChange) \n",
    " \n",
    "labelClass = tkinter.Label(root, \n",
    "                              text='Class:', \n",
    "                              justify=tkinter.RIGHT, width=50) \n",
    "labelClass.place(x=130, y=40, width=50, height=20) \n",
    "#学生班级组合框 \n",
    "comboClass = tkinter.ttk.Combobox(root, width=50) \n",
    "comboClass.place(x=190, y=40, width=50, height=20) \n",
    " \n",
    "labelSex = tkinter.Label(root, \n",
    "                            text='Sex:', \n",
    "                            justify=tkinter.RIGHT, width=50) \n",
    "labelSex.place(x=10, y=70, width=50, height=20) \n",
    "#与性别关联的变量，1:男；0:女，默认为男 \n",
    "sex = tkinter.IntVar() \n",
    "sex.set(1) \n",
    "#单选钮，男 \n",
    "radioMan = tkinter.Radiobutton(root, \n",
    "                                  variable=sex, \n",
    "                                  value=1, \n",
    "                                  text='Man') \n",
    "radioMan.place(x=70, y=70, width=50, height=20) \n",
    "#单选钮，女 \n",
    "radioWoman = tkinter.Radiobutton(root, \n",
    "                                    variable=sex, \n",
    "                                    value=0, \n",
    "                                    text='Woman') \n",
    "radioWoman.place(x=130, y=70, width=70, height=20) \n",
    " \n",
    "#与是否班长关联的变量，默认当前学生不是班长 \n",
    "monitor = tkinter.IntVar() \n",
    "monitor.set(0) \n",
    "#复选框，选中时变量值为1，未选中时变量值为0 \n",
    "checkMonitor = tkinter.Checkbutton(root, \n",
    "                                      text='Is Monitor?',  \n",
    "                                      variable=monitor, \n",
    "                                      onvalue=1, \n",
    "                                      offvalue=0) \n",
    "checkMonitor.place(x=20, y=100, width=100, height=20) \n",
    "#添加按钮单击事件处理函数 \n",
    "def addInformation(): \n",
    "    result = 'Name:' + entryName.get() \n",
    "    result = result + ';Grade:' + comboGrade.get() \n",
    "    result = result + ';Class:' + comboClass.get() \n",
    "    result=result+'_sex:'\\\n",
    "        +('man'if sex.get()else'woman')\n",
    "    result=result+'_monitor:'\\\n",
    "        +('yes'if monitor.get()else'no')\n",
    "\n",
    "    #把信息插入到列表框组件中 \n",
    "    listboxStudents.insert(0, result)     \n",
    "buttonAdd = tkinter.Button(root, \n",
    "                             text='Add', \n",
    "                             width=40, \n",
    "                             command=addInformation) \n",
    "buttonAdd.place(x=130, y=100, width=40, height=20) \n",
    " \n",
    "#删除按钮的事件处理函数 \n",
    "def deleteSelection(): \n",
    "    selection = listboxStudents.curselection() \n",
    "    if  not selection: \n",
    "        tkinter.messagebox.showinfo(title='Information', \n",
    "                                       message='No Selection') \n",
    "    else: \n",
    "        listboxStudents.delete(selection) \n",
    "buttonDelete = tkinter.Button(root, \n",
    "                                 text='DeleteSelection', \n",
    "                                 width=100, \n",
    "                                 command=deleteSelection) \n",
    "buttonDelete.place(x=180, y=100, width=100, height=20) \n",
    " \n",
    "#创建列表框组件 \n",
    "listboxStudents = tkinter.Listbox(root, width=300) \n",
    "listboxStudents.place(x=10, y=130, width=300, height=200) \n",
    " \n",
    "#启动消息循环 \n",
    "root.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
